﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySite.Model;
using MySite.DAL;
using System.Web;

namespace MySite.BLL
{
    public class Visit
    {
        #region 更新微博信息
        /// <summary>
        /// 更新微博信息(插入和更新)
        /// </summary>
        /// <param name="vinfo"></param>
        /// <returns></returns>
        public ReturnMessage UpdateVisit(VisitInfo vinfo)
        {
            ReturnMessage rm = new ReturnMessage(true);
            VisitDAL sdal = new VisitDAL();
            rm = sdal.Exist(vinfo);
            if (rm.IsSuccessed)
            {
                vinfo.PID = Convert.ToInt32(rm.ReturnData["PID"]);
                rm = sdal.Update(vinfo);
            }
            else
            {
                rm = sdal.Add(vinfo);
            }

            return rm;
        }
        #endregion

        #region 更新微博信息
        /// <summary>
        /// 更新微博信息
        /// </summary>
        /// <param name="siList"></param>
        /// <returns></returns>
        public ReturnMessage UpdateVisit(IList<VisitInfo> siList)
        {
            ReturnMessage rm = new ReturnMessage(true);

            try
            {
                foreach (var item in siList)
                {
                    rm = UpdateVisit(item);

                    if (!rm.IsSuccessed)
                    {
                        return rm;
                    }
                }
            }
            catch (Exception e)
            {
                rm.IsSuccessed = false;
                rm.Exception = e;
            }

            return rm;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<VisitInfo> Query(string where)
        {
            return new VisitDAL().Query(where);
        }

        public IList<VisitInfo> Query(string where, int top)
        {
            return new VisitDAL().Query(where, top);
        }

        public IList<VisitInfo> Query(string where, string orderBy, int pageIndex, int pageSize, ref int pageCount)
        {
            return new VisitDAL().Query(where, pageSize, pageIndex, orderBy, ref pageCount);
        }
        #endregion

        public void General(HttpContext content)
        {
            VisitInfo vi = new VisitInfo();
            vi.VisitIP = content.Request.UserHostAddress;
            vi.VisitBrowser = content.Request.Browser.Browser;
            vi.VisitCount = 0;
            vi.VisitOS = content.Request.Browser.Platform;
            vi.VisitResolution = new ResolutionInfo(Convert.ToInt32(content.Request.QueryString["width"]), Convert.ToInt32(content.Request.QueryString["height"]));
            vi.VisitSourceUrl = content.Request.Url.ToString();
            vi.VisitTime = DateTime.Now;
            vi.VisitUserName = content.User.Identity.Name;

            UpdateVisit(vi);
        }
    }
}
